<template>
  <div class="Cangkushezhi">
    <div class="sTop">
      &nbsp;&nbsp;&nbsp;&nbsp;部门名称：<el-input
        placeholder="请输入部门名称"
        v-model="input"
        size="small"
        clearable
      ></el-input>
      &nbsp;<el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        class="button1 detail"
        @click="searchPart"
        >查询</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-refresh-right"
        class="button1 rePassword"
        @click="reload"
        >重置</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        class="button2 addUser"
        @click="centerDialogVisible2 = true"
        >添加部门</el-button
      >
    </div>

    <el-table
      :data="tableData"
      border
      stripe
      :row-style="{ height: '60px', padding: '0 0' }"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection"> </el-table-column>
      <el-table-column
        prop="partName"
        label="部门名称"
        align="center"
        width="250px"
      >
      </el-table-column>
      <el-table-column prop="partDescription" label="描述" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="scope">
          <el-button
            class="addUser"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            class="rePassword"
            size="mini"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :page-size="pageShow"
      :current-page="pageNow"
      layout="prev, pager, next,sizes"
      :page-sizes="[2, 3, 5, 10]"
      @current-change="pageChange"
      @size-change="sizeChange"
      @prev-click="back()"
      @next-click="next()"
      :total="pageNumber"
    >
    </el-pagination>
    <!-- 添加新部门对话框 -->
    <el-dialog
      title="添加部门"
      :visible.sync="centerDialogVisible2"
      width="30%"
      center
    >
      <el-form
        :model="insertOneArr"
        ref="insertOneArr"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input
            type="age"
            v-model="insertOneArr.partName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="insertOneArr.partDescription"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPart">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改部门信息 -->
    <el-dialog
      title="修改部门信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <el-form
        :model="managerInformation"
        ref="managerInformation"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="部门编号" prop="id">
          <el-input
            type="name"
            v-model.number="managerInformation.partId"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="仓库名称" prop="name">
          <el-input
            type="age"
            v-model="managerInformation.partName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="managerInformation.partDescription"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除管理员对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <i class="el-icon-warning"></i><span>确认删除此部门吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteManager">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import yuangong from "@/api/yuangong.js";
export default {
  data() {
    return {
      input: "", //搜索用
      partId: "", //删除用 部门ID
      pageNow: 1 /* 当前页数 */,
      pageNumber: 50 /* 总页数 */,
      pageShow: 10 /* 每页显示个数 */,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      dialogVisible: false,
      insertOneArr: {
        partName: "",
        partDescription: "",
      }, //添加用对象
      managerArr: [],
      tableData: [], //渲染到表格的数据
      options: [
        {
          //select 选项
          value: "1",
          label: "自家仓库",
        },
        {
          value: "2",
          label: "电力公司仓库",
        },
      ],
      value: "", //select当前选定的 option
      managerInformation: {},
      rules: {},
    };
  },
  created() {
    yuangong.getPartList(this.pageNow, this.pageShow).then((res) => {
      /* console.log(res); */
      this.managerArr = res.data.data.list;
      this.pageNumber = res.data.data.total;
      this.tableData = this.managerArr;
      /* console.log(this.tableData); */
    });
  },
  methods: {
    //pageSize改变
    sizeChange(i) {
      this.pageShow = i;
      //获取用户列表
      yuangong.getPartList(this.pageNow, this.pageShow).then((res) => {
        /* console.log(res); */
        this.managerArr = res.data.data.list;
        this.pageNumber = res.data.data.total;
        this.tableData = this.managerArr;
        /* console.log(this.tableData); */
      });
    },
    //点击页码
    pageChange(i) {
      this.pageNow = i;
      //获取用户列表
      yuangong.getPartList(this.pageNow, this.pageShow).then((res) => {
        /* console.log(res); */
        this.managerArr = res.data.data.list;
        this.pageNumber = res.data.data.total;
        this.tableData = this.managerArr;
        /* console.log(this.tableData); */
      });
    },
    //上一页
    back() {
      this.pageNow--;
    },
    //下一页
    next() {
      this.pageNow++;
    },
    /* 修改 */
    handleEdit(i, v) {
      this.centerDialogVisible = true;
      this.managerInformation = v;
    },

    /*修改*/
    update() {
      this.centerDialogVisible = false;
      yuangong.updatePart(this.managerInformation).then((res) => {
        /* console.log(res); */
        location.reload();
      });
    },
    // 新增部门
    addPart() {
      yuangong.addPart(this.insertOneArr).then((res) => {
        /* console.log(res); */
      });
      this.centerDialogVisible2 = false;
      location.reload();
    },
    /*删除部门*/
    handleDelete(i, v) {
      this.dialogVisible = true;
      this.partId = v.partId;
    },
    /*删除部门*/
    deleteManager() {
      this.dialogVisible = false;
      yuangong.deletePart(this.partId).then((res) => {
        /* console.log(res); */
        location.reload();
      });
    },
    //搜索部门
    searchPart() {
      yuangong.searchPart(1, 10, this.input).then((res) => {
        /* console.log(res); */
        this.tableData = res.data.data.list;
        this.pageNumber = res.data.data.total;
        /* console.log(this.tableData); */
      });
    },
    //重置
    reload() {
      //获取部门列表
      yuangong.getPartList(this.pageNow, this.pageShow).then((res) => {
        /* console.log(res); */
        this.managerArr = res.data.data.list;
        this.pageNumber = res.data.data.total;
        this.tableData = this.managerArr;

        this.input = "";
      });
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.Cangkushezhi {
  background-color: white;
}
.el-input {
  width: 200px;
  margin-right: 10px;
}
.sTop {
  padding-top: 20px;
  font-size: 14px;
  margin-bottom: 40px;
}
.el-table thead {
  background-color: #f5f7fa;
}
.has-gutter .is-leaf .cell {
  text-align: center;
  padding-left: 10px;
  width: 150px;
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-icon-warning {
  color: orange;
  font-size: 20px;
  margin-right: 5px;
}

.sTop >>> .button1 {
  text-align: center;
  width: 78px;
  height: 32px;
}
.sTop >>> .button2 {
  text-align: center;
  width: 118px;
  height: 32px;
}
.sTop >>> .button1,
.button2 {
  font-size: 14px;
}
.is-plain {
  width: 80px;
}
.dialog-footer .el-button--default,
.el-button--primary {
  width: 74px;
  height: 40px;
}
</style>